unit Class_DBPool;

interface

uses
  SysUtils, ADODB;

type
  TDBPool = class(TObject)
  public
    FConnString: string;
    FPath: string;
  public
    function GetConnect(AOpenCode: Integer=0;
      PrintErrorState: Boolean=True): TADOConnection; overload;

    class function GetConnect(AConnString: string; AOpenCode: Integer=0;
      PrintErrorState: Boolean=True): TADOConnection; overload;
    class procedure OpenConnect(var AADO: TADOConnection;
      PrintErrorState: Boolean=True);
    class procedure UpdateConnString(var ADBPool: TDBPool; AConnString: string);
  end;

implementation

uses
  ComUtils;

{ TDBPool }

function TDBPool.GetConnect(AOpenCode: Integer; PrintErrorState: Boolean): TADOConnection;
begin
   Result := GetConnect(FConnString, AOpenCode, PrintErrorState);
end;

class function TDBPool.GetConnect(AConnString: string;
  AOpenCode: Integer; PrintErrorState: Boolean): TADOConnection;
begin
  Result := nil;
  if AConnString = '' then Exit;
  Result := TADOConnection.Create(nil);
  Result.ConnectionString := AConnString;
  if AOpenCode <> 0 then OpenConnect(Result, PrintErrorState);
end;

class procedure TDBPool.OpenConnect(var AADO: TADOConnection; PrintErrorState: Boolean);
begin
  try
    AADO.Open();
  except
    on E: Exception do
    begin
      if PrintErrorState then
      begin
        PrintMsg(Self, E.Message);
      end;
      FreeAndNil(AADO);
    end;
  end;
end;

class procedure TDBPool.UpdateConnString(var ADBPool: TDBPool;
  AConnString: string);
begin
  if ADBPool = nil then ADBPool := TDBPool.Create;
  ADBPool.FConnString := AConnString;
end;

end.
